package com.youdianstar.app.common.util;

import android.annotation.SuppressLint;
import android.text.TextUtils;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * File: StrUtil
 * Author: 82149 Create: 2019/2/22 14:51
 * Changes (from 2019/2/22)
 * --------------------------------------------------
 * describe:
 * ---------------------------------------------------
 */
public class StrUtil {
    /**
     * 描述：判断一个字符串是否为null或空值.
     *
     * @param str 指定的字符串
     * @return true or false
     */
    public static boolean isEmpty(String str) {
        List<String> pinArray = new ArrayList<>();
        pinArray.add("6");
        return str == null || str.trim().length() == 0 || "null".equals(str.trim());
    }

    /**
     * 描述：将null转化为“”.
     *
     * @param str 指定的字符串
     * @return 字符串的String类型
     */
    public static String parseEmpty(String str) {
        if (str == null || "null".equals(str.trim()) || "".equals(str)) {
            str = "";
        }
        return str.trim();
    }

    /**
     * 描述：将null转化为0.
     *
     * @param lo data
     * @return
     */
    public static Long parseEmpty(Long lo) {
        if (null == lo) {
            return Long.parseLong("0");
        }
        return lo;
    }

    /**
     * 半角转换为全角
     *
     * @param input
     * @return
     */
    public static String parseDBC(String input) {
        if (null == input) {
            return "";
        }
        TextUtils.replace(input,new String[]{" ","\u3000"},new CharSequence[]{"\u00A0","\u00A0"});
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }


    /**
     * 银行卡中间八个字符隐藏为*
     *
     * @param bankCardNum
     */
    public static String hideBankCardNum(String bankCardNum) {
        try {
            if (bankCardNum == null) return "";

            int length = bankCardNum.length();

            if (length > 4) {
                String startNum = bankCardNum.substring(0, 4);
                String endNum = bankCardNum.substring(length - 4, length);
                bankCardNum = startNum + " **** **** " + endNum;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bankCardNum;
    }



    /**
     * 隐藏手机号
     * 脱敏规则-只显示后四位
     * @param phone 原手机号
     */
    public static String hidePhone(String phone) {
        String hide = null;
        if (phone.length() > 4) {
            StringBuilder hideBuild = new StringBuilder();
            for (int index = 0 ; index<phone.length()-4; index++){
                hideBuild.append("*");
            }
            hide= hideBuild.toString()+phone.substring(phone.length()-4);
        } else{
            hide = phone;//phone.replaceAll("(\\d)\\d{4}(\\d)", "$1****$2");
        }
        return hide;
    }

    /**
     * 隐藏邮箱
     * 脱敏规则-@符号前文本只显示前3位,@后面全显示
     * @param email 邮箱
     */
    public static String hideEmail(String email) {
        if (!email.contains("@"))
            return email;
        try {
            String emailStr = email.substring(0, email.lastIndexOf("@"));
            String hide = null;
            if (emailStr.length()>3){
                StringBuilder hideBuild = new StringBuilder();
                for (int index = 3 ; index<emailStr.length(); index++){
                    hideBuild.append("*");
                }
                hide= email.substring(0, 3)+hideBuild.toString()+email.substring(email.lastIndexOf("@"));
            }else {
                hide = email;
            }
            return hide;
        }catch (Exception e){
            return email;
        }
    }

    /**
     * 校验EMAIL格式，真为正确
     *
     * @param email 邮件地址
     * @return true 为格式正确 false 为格式错误
     */
    public static boolean emailFormat(String email) {
        Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
        Matcher mc = pattern.matcher(email);
        return mc.matches();
    }

    /**
     * 是否只包含数字和字母
     *
     * @param str
     * @return
     */
    public static boolean isLetterDigit(String str) {
        String regex = "^[a-z0-9A-Z]+$";
        return str.matches(regex);
    }


    /**
     * 利用正则表达式 判断字符串是否整数
     *
     * @param data
     */
    public static boolean isIntFormat(String data){
        if (isEmpty(data))
            return false;
        Pattern pattern = Pattern.compile("-?\\d+$");
        Matcher isNum = pattern.matcher(data);
        return isNum.matches();
    }

    /**
     * 判断是否 Double字符
     *
     * @param data
     */
    public static boolean isDoubleFormat(String data){
        if (isEmpty(data))
            return false;
        Pattern pattern = Pattern.compile("[+-]?\\d+(.\\d+)?");
        return pattern.matcher(data).matches();
    }





    /*********************************************************************************
     * ****** app 业务使用
     * *****/

    /**
     * 方法: getRandomString <p>
     * 描述: 获取随机唯一字符串<p>
     * 参数: @return<p>
     */
    public static String getRandomStr() {
        String hexUUID = new String(UUID.randomUUID().toString());
        String nameString = System.nanoTime() + "";
        return hexUUID + nameString;
    }


    /**
     * 校验口令码是否符合规则
     *
     * @param code str
     */
    public static boolean passCodeFormat(String code){
        if (null==code) return  false;
//         Pattern pattern = Pattern.compile("code-[a-zA-Z0-9]{8}");
        Pattern pattern = Pattern.compile("^\\[code-\\w+\\]$");
        Matcher mc = pattern.matcher(code);
        return mc.matches();
    }

    /**
     * Transform int to String with prefix "0" if less than 10
     *
     * @param num int [0 ...30]
     */
    public static String format2LenStr(int num) {
        return (num < 10) ? "0" + num : String.valueOf(num);
    }

    /**
     * 8 位数字补0
     *
     * @param num 数字<8位
     */
    public static String format8LenStr(int num) {
        String numStr = String.valueOf(num);
        if (numStr.length()>=8)
            return numStr;
        else {
            return String.format("%08d",num);
        }
    }

    /**显示卡号 后四位*/
    public static String showCardNormal(String card) {
        return "****  ****  **** " + card;
    }

    /**
     * 获取Credit card
     * ExpDate : MMyy
     *
     * @param year 20xx
     * @param month 1-12
     */
    public static String getCardExp(Integer year,Integer month){
        int yearNum=0;
        int monthNum =0;
        if (year!=null)
            yearNum = year;
        if (month!=null)
            monthNum = month;
        return format2LenStr(monthNum)+String.valueOf(yearNum).substring(2);
    }

    /**
     * 获取Credit card
     * ExpDate : MM/yyyy -- 上传接口用
     *
     * @param year 20xx
     * @param month 1-12
     */
    public static String setCardBindExp(Integer year,Integer month){
        int yearNum=0;
        int monthNum =0;
        if (year!=null)
            yearNum = year;
        if (month!=null)
            monthNum = month;
        if (yearNum>999){
            return format2LenStr(monthNum)+"/"+ yearNum;
        }else {
            return format2LenStr(monthNum)+"/"+ yearNum;
        }
    }


    /**
     * 替换所有的空格
     */
    public static String parseAllSpace(String s) {
        if (isEmpty(s))
            return "";
        return s.replaceAll(" ", "");
    }


    public static String distance2Str(Double distance) {
        if (distance == 0) return "0m";
        if (distance > 1) {
            return distance + "Km";
        }
        return (int) (distance * 1000) + "m";
    }

}
